package com.zhenhao.xsq.view;

import android.app.Dialog;
import android.content.Context;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @author xlei
 * @Date 2018/7/9.
 */

public class GwchangeDialog extends Dialog {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_ymm)
    EditText etYmm;
    @BindView(R.id.ll_ymm)
    LinearLayout llYmm;
    @BindView(R.id.et_xmm)
    EditText etXmm;
    @BindView(R.id.ll_xmm)
    LinearLayout llXmm;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    private onSureListene listene;

    public GwchangeDialog(Context context, int type, onSureListene sure) {
        super(context, R.style.BaseDialog);
        initView(type, sure);
    }

    private void initView(int type, final onSureListene listener) {
        View dialog_view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_gwchangepwd,
                null);
        setContentView(dialog_view);
        ButterKnife.bind(this, dialog_view);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        switch (type) {
            case 1:
                //修改名字 gwell 萤石
                tvTitle.setText("修改设备名字");
                etName.setFilters(new InputFilter[] { new InputFilter.LengthFilter(20) });
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(etName.getText().toString())) {
                            ToastUtil.showLong("请输入新名字");
                            return;
                        }
                        dismiss();
                        listener.onSureListene(etName.getText().toString(), "");

                    }
                });
                break;
            case 2:
                //修改密码
                tvTitle.setText("修改密码");
                etName.setVisibility(View.GONE);
                llXmm.setVisibility(View.VISIBLE);
                llYmm.setVisibility(View.VISIBLE);
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(etXmm.getText().toString())) {
                            ToastUtil.showLong("请输入新密码");
                            return;
                        }
                        if (!RegexUtils.isMatch("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$", etXmm
                                .getText().toString())) {
                            ToastUtil.showLong("必须包含字母和数字，并6-18位");
                            return;
                        }
                        dismiss();
                        listener.onSureListene(etXmm.getText().toString(), "");
                    }
                });
                break;
            case 3:
                tvTitle.setText("修改设备位置");
                etName.setHint("请输入新位置");
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(etName.getText().toString())) {
                            ToastUtil.showLong("请输入所在位置");
                            return;
                        }
                        dismiss();
                        listener.onSureListene(etName.getText().toString(), "");

                    }
                });
                break;
            case 4:
                tvTitle.setText("视频已加密");
                etName.setHint("请输入验证码");
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(etName.getText().toString())) {
                            ToastUtil.showLong("请输入验证码");
                            return;
                        }
                        dismiss();
                        listener.onSureListene(etName.getText().toString(), "");

                    }
                });
                break;
            case 5:
                //修改备注信息
                tvTitle.setText("修改备注信息");
                etName.setHint("请输入备注信息");
                etName.setFilters(new InputFilter[] { new InputFilter.LengthFilter(200) });
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(etName.getText().toString())) {
                            ToastUtil.showLong("请输入备注");
                            return;
                        }
                        dismiss();
                        listener.onSureListene(etName.getText().toString(), "");

                    }
                });
                break;
            case 6:
                //修改备注信息
                tvTitle.setText("备  注");
                etName.setHint("输入拒绝理由");
                etName.setFilters(new InputFilter[] { new InputFilter.LengthFilter(200) });
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(etName.getText().toString())) {
                            ToastUtil.showLong("请输入备注");
                            return;
                        }
                        dismiss();
                        listener.onSureListene(etName.getText().toString(), "");
                    }
                });
                break;

        }
        show();
    }

    public void setYmm(String ymm) {
        etYmm.setText(ymm);
    }

    public interface onSureListene {
        void onSureListene(String str1, String str2);
    }
}
